// pages/message/message.js


import {
    GetHightNewsType, //资讯分类
    GetHomeList, // 资讯列表
} from '../../api/index'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
        listArray:[],
        dataSourceArray:[],
                    pageNo:1,
                    totalPages:0,
                    nomore:false,
                    selectTabId:'',
                    searchString:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
        this._GetHightNewsType('', false)
  },

  // 搜索
  handleSearch(e) {
    var searchValue = e.detail.value;
    // 在这里执行搜索逻辑
    console.log('用户点击了搜索，输入的内容为：' + searchValue);

    this.setData({
        searchString: searchValue, // 更新对应的data中的值
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false
      })

      this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10&newsFrom=' + this.data.selectTabId + '&title=' + this.data.searchString, 2, false)
  },

  // 输入框输入事件
  inputEvent(e) {
    var value = e.detail.value;
    this.setData({
        searchString: value // 更新对应的data中的值
    });
  },

  // 跳转详情
  cellSelectAction(event) {
    wx.navigateTo({
        url: '/pages/index/examinationDetail/examinationDetail?id=' + event.currentTarget.dataset.id,
      })
  },

  // tab切换事件
  onChange(event) {
      console.log(event);
    let selectIndex = event.detail.index;
    this.setData({
        searchString:'',
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
        selectTabId:this.data.dataSourceArray[selectIndex].id,
      })

      this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10&newsFrom=' + this.data.selectTabId + '&title=' + this.data.searchString, 2, false)

},

  // 资讯分类
  _GetHightNewsType(data,isLoading) {
    GetHightNewsType(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                dataSourceArray:res.rows,
                selectTabId:res.rows[0].id
            })
            this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10&newsFrom=' + this.data.selectTabId + '&title=' + this.data.searchString, 2, true)
        } else {
            Toast(res.msg)
        }
    })
},

_GetHomeList(data, type,isLoading) {
    console.log(data)
    console.log(this.data.listArray)
    let that = this
    if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        console.log('对吗')
        this.setData({
          nomore: true,
          changePage:true
        })
        return;
      }
      console.log('type == ' + type)
      GetHomeList(data, type, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            that.setData({
                changePage:true,
                listArray: that.data.listArray.concat(array),
                pageNo: that.data.pageNo + 1,
                totalPages: res.total,
              });
 
              if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                    that.setData({
                        nomore: true
                    })
              } 
        } else {
            Toast(res.msg)
        }
    })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉刷新')
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10&newsFrom=' + this.data.selectTabId + '&title=' + this.data.searchString, 2, false)
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('上拉加载更多')
    this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10&newsFrom=' + this.data.selectTabId + '&title=' + this.data.searchString, 2, false)
  },

  onShareTimeline: function (){

},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})